home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclrect.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  20.1 KB  |  703 lines

  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclrect.c,v 1.2 2000/09/19 19:00:35 lpd Exp $ */
  20. /* Rectangle-oriented command writing for command list */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsutil.h"        /* for gs_next_ids */
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"        /* must precede gxcldev.h */
  26. #include "gxcldev.h"
  27.  
  28. /* ---------------- Writing utilities ---------------- */
  29.  
  30. #define cmd_set_rect(rect)\
  31.   ((rect).x = x, (rect).y = y,\
  32.    (rect).width = width, (rect).height = height)
  33.  
  34. /* Write a rectangle. */
  35. private int
  36. cmd_size_rect(register const gx_cmd_rect * prect)
  37. {
  38.     return
  39.     cmd_sizew(prect->x) + cmd_sizew(prect->y) +
  40.     cmd_sizew(prect->width) + cmd_sizew(prect->height);
  41. }
  42. private byte *
  43. cmd_put_rect(register const gx_cmd_rect * prect, register byte * dp)
  44. {
  45.     cmd_putw(prect->x, dp);
  46.     cmd_putw(prect->y, dp);
  47.     cmd_putw(prect->width, dp);
  48.     cmd_putw(prect->height, dp);
  49.     return dp;
  50. }
  51.  
  52. int
  53. cmd_write_rect_cmd(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  54.            int op, int x, int y, int width, int height)
  55. {
  56.     int dx = x - pcls->rect.x;
  57.     int dy = y - pcls->rect.y;
  58.     int dwidth = width - pcls->rect.width;
  59.     int dheight = height - pcls->rect.height;
  60.     byte *dp;
  61.     int code;
  62.  
  63. #define check_range_xy(rmin, rmax)\
  64.   ((unsigned)(dx - rmin) <= (rmax - rmin) &&\
  65.    (unsigned)(dy - rmin) <= (rmax - rmin))
  66. #define check_range_w(rmin, rmax)\
  67.   ((unsigned)(dwidth - rmin) <= (rmax - rmin))
  68. #define check_ranges(rmin, rmax)\
  69.   (check_range_xy(rmin, rmax) && check_range_w(rmin, rmax) &&\
  70.    (unsigned)(dheight - rmin) <= (rmax - rmin))
  71.     cmd_set_rect(pcls->rect);
  72.     if (dheight == 0 && check_range_w(cmd_min_dw_tiny, cmd_max_dw_tiny) &&
  73.     check_range_xy(cmd_min_dxy_tiny, cmd_max_dxy_tiny)
  74.     ) {
  75.     byte op_tiny = op + 0x20 + dwidth - cmd_min_dw_tiny;
  76.  
  77.     if (dx == width - dwidth && dy == 0) {
  78.         code = set_cmd_put_op(dp, cldev, pcls, op_tiny + 8, 1);
  79.         if (code < 0)
  80.         return code;
  81.     } else {
  82.         code = set_cmd_put_op(dp, cldev, pcls, op_tiny, 2);
  83.         if (code < 0)
  84.         return code;
  85.         dp[1] = (dx << 4) + dy - (cmd_min_dxy_tiny * 0x11);
  86.     }
  87.     }
  88. #define rmin cmd_min_short
  89. #define rmax cmd_max_short
  90.     else if (check_ranges(rmin, rmax)) {
  91.     int dh = dheight - cmd_min_dxy_tiny;
  92.  
  93.     if ((unsigned)dh <= cmd_max_dxy_tiny - cmd_min_dxy_tiny &&
  94.         dh != 0 && dy == 0
  95.         ) {
  96.         op += dh;
  97.         code = set_cmd_put_op(dp, cldev, pcls, op + 0x10, 3);
  98.         if (code < 0)
  99.         return code;
  100.         if_debug3('L', "    rs2:%d,%d,0,%d\n",
  101.               dx, dwidth, dheight);
  102.     } else {
  103.         code = set_cmd_put_op(dp, cldev, pcls, op + 0x10, 5);
  104.         if (code < 0)
  105.         return code;
  106.         if_debug4('L', "    rs4:%d,%d,%d,%d\n",
  107.               dx, dwidth, dy, dheight);
  108.         dp[3] = dy - rmin;
  109.         dp[4] = dheight - rmin;
  110.     }
  111.     dp[1] = dx - rmin;
  112.     dp[2] = dwidth - rmin;
  113.     }
  114. #undef rmin
  115. #undef rmax
  116.     else if (dy >= -2 && dy <= 1 && dheight >= -2 && dheight <= 1 &&
  117.          (dy + dheight) != -4
  118.     ) {
  119.     int rcsize = 1 + cmd_sizew(x) + cmd_sizew(width);
  120.  
  121.     code = set_cmd_put_op(dp, cldev, pcls,
  122.                   op + ((dy + 2) << 2) + dheight + 2, rcsize);
  123.     if (code < 0)
  124.         return code;
  125.     ++dp;
  126.     cmd_put2w(x, width, dp);
  127.     } else {
  128.     int rcsize = 1 + cmd_size_rect(&pcls->rect);
  129.  
  130.     code = set_cmd_put_op(dp, cldev, pcls, op, rcsize);
  131.     if (code < 0)
  132.         return code;
  133.     if_debug5('L', "    r%d:%d,%d,%d,%d\n",
  134.           rcsize - 1, dx, dwidth, dy, dheight);
  135.     cmd_put_rect(&pcls->rect, dp + 1);
  136.     }
  137.     return 0;
  138. }
  139.  
  140. /* ---------------- Driver procedures ---------------- */
  141.  
  142. int
  143. clist_fill_rectangle(gx_device * dev, int x, int y, int width, int height,
  144.              gx_color_index color)
  145. {
  146.     gx_device_clist_writer * const cdev =
  147.     &((gx_device_clist *)dev)->writer;
  148.     int code;
  149.  
  150.     fit_fill(dev, x, y, width, height);
  151.     FOR_RECTS {
  152.     pcls->colors_used.or |= color;
  153.     TRY_RECT {
  154.         code = cmd_disable_lop(cdev, pcls);
  155.         if (code >= 0 && color != pcls->colors[1])
  156.         code = cmd_put_color(cdev, pcls, &clist_select_color1,
  157.                      color, &pcls->colors[1]);
  158.         if (code >= 0)
  159.         code = cmd_write_rect_cmd(cdev, pcls, cmd_op_fill_rect, x, y,
  160.                       width, height);
  161.     } HANDLE_RECT(code);
  162.     } END_RECTS;
  163.     return 0;
  164. }
  165.  
  166. int
  167. clist_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tile,
  168.                int x, int y, int width, int height,
  169.            gx_color_index color0, gx_color_index color1, int px, int py)
  170. {
  171.     gx_device_clist_writer * const cdev =
  172.     &((gx_device_clist *)dev)->writer;
  173.     int depth =
  174.     (color1 == gx_no_color_index && color0 == gx_no_color_index ?
  175.      dev->color_info.depth : 1);
  176.     gx_color_index colors_used =
  177.     (color1 == gx_no_color_index && color0 == gx_no_color_index ?
  178.      /* We can't know what colors will be used: assume the worst. */
  179.      ((gx_color_index)1 << depth) - 1 :
  180.      (color0 == gx_no_color_index ? 0 : color0) |
  181.      (color1 == gx_no_color_index ? 0 : color1));
  182.     int code;
  183.  
  184.     fit_fill(dev, x, y, width, height);
  185.     FOR_RECTS {
  186.     ulong offset_temp;
  187.  
  188.     pcls->colors_used.or |= colors_used;
  189.     TRY_RECT {
  190.         code = cmd_disable_lop(cdev, pcls);
  191.     } HANDLE_RECT(code);
  192.     if (!cls_has_tile_id(cdev, pcls, tile->id, offset_temp)) {
  193.         code = 0;
  194.         if (tile->id != gx_no_bitmap_id) {
  195.         TRY_RECT {
  196.             code = clist_change_tile(cdev, pcls, tile, depth);
  197.         } HANDLE_RECT_UNLESS(code,
  198.             (code != gs_error_VMerror || !cdev->error_is_retryable));
  199.         }
  200.         if (code < 0) {
  201.         /* ok if gx_default... does retries internally: */
  202.         /* it's self-sufficient */
  203.         code = gx_default_strip_tile_rectangle(dev, tile,
  204.                                x, y, width, height,
  205.                                color0, color1,
  206.                                px, py);
  207.         if (code < 0)
  208.             ERROR_RECT(code);
  209.         goto endr;
  210.         }
  211.     }
  212.     TRY_RECT {
  213.         code = 0;
  214.         if (color0 != pcls->tile_colors[0] || color1 != pcls->tile_colors[1])
  215.         code = cmd_set_tile_colors(cdev, pcls, color0, color1);
  216.         if (px != pcls->tile_phase.x || py != pcls->tile_phase.y) {
  217.         if (code >= 0)
  218.             code = cmd_set_tile_phase(cdev, pcls, px, py);
  219.         }
  220.         if (code >= 0)
  221.         code = cmd_write_rect_cmd(cdev, pcls, cmd_op_tile_rect, x, y,
  222.                       width, height);
  223.     } HANDLE_RECT(code);
  224. endr:;
  225.     } END_RECTS;
  226.     return 0;
  227. }
  228.  
  229. int
  230. clist_copy_mono(gx_device * dev,
  231.         const byte * data, int data_x, int raster, gx_bitmap_id id,
  232.         int x, int y, int width, int height,
  233.         gx_color_index color0, gx_color_index color1)
  234. {
  235.     gx_device_clist_writer * const cdev =
  236.     &((gx_device_clist *)dev)->writer;
  237.     int y0;
  238.     gx_bitmap_id orig_id = id;
  239.     gx_color_index colors_used =
  240.     (color0 == gx_no_color_index ? 0 : color0) |
  241.     (color1 == gx_no_color_index ? 0 : color1);
  242.  
  243.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  244.     y0 = y;
  245.     FOR_RECTS {
  246.     int dx = data_x & 7;
  247.     int w1 = dx + width;
  248.     const byte *row = data + (y - y0) * raster + (data_x >> 3);
  249.     int code;
  250.  
  251.     pcls->colors_used.or |= colors_used;
  252.     TRY_RECT {
  253.         code = cmd_disable_lop(cdev, pcls);
  254.         if (code >= 0)
  255.         code = cmd_disable_clip(cdev, pcls);
  256.         if (color0 != pcls->colors[0] && code >= 0)
  257.         code = cmd_set_color0(cdev, pcls, color0);
  258.         if (color1 != pcls->colors[1] && code >= 0)
  259.         code = cmd_set_color1(cdev, pcls, color1);
  260.     } HANDLE_RECT(code);
  261.     /* Don't bother to check for a possible cache hit: */
  262.     /* tile_rectangle and fill_mask handle those cases. */
  263. copy:{
  264.     gx_cmd_rect rect;
  265.     int rsize;
  266.     byte op = (byte) cmd_op_copy_mono;
  267.     byte *dp;
  268.     uint csize;
  269.     uint compress;
  270.     int code;
  271.  
  272.     rect.x = x, rect.y = y;
  273.     rect.width = w1, rect.height = height;
  274.     rsize = (dx ? 3 : 1) + cmd_size_rect(&rect);
  275.     TRY_RECT {
  276.         code = cmd_put_bits(cdev, pcls, row, w1, height, raster,
  277.                 rsize, (orig_id == gx_no_bitmap_id ?
  278.                     1 << cmd_compress_rle :
  279.                     cmd_mask_compress_any),
  280.                 &dp, &csize);
  281.     } HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);
  282.     compress = (uint)code;
  283.     if (code < 0) {
  284.         /* The bitmap was too large; split up the transfer. */
  285.         if (height > 1) {
  286.         /*
  287.          * Split the transfer by reducing the height.
  288.          * See the comment above FOR_RECTS in gxcldev.h.
  289.          */
  290.         height >>= 1;
  291.         goto copy;
  292.         } else {
  293.         /* Split a single (very long) row. */
  294.         int w2 = w1 >> 1;
  295.  
  296.         NEST_RECT {
  297.             code = clist_copy_mono(dev, row, dx,
  298.                        raster, gx_no_bitmap_id, x, y,
  299.                        w2, 1, color0, color1);
  300.             if (code >= 0)
  301.             code = clist_copy_mono(dev, row, dx + w2,
  302.                            raster, gx_no_bitmap_id,
  303.                            x + w2, y,
  304.                            w1 - w2, 1, color0, color1);
  305.         } UNNEST_RECT;
  306.         if (code < 0)
  307.             ERROR_RECT(code);
  308.         continue;
  309.         }
  310.     }
  311.     op += compress;
  312.     if (dx) {
  313.         *dp++ = cmd_count_op(cmd_opv_set_misc, 2);
  314.         *dp++ = cmd_set_misc_data_x + dx;
  315.     }
  316.     *dp++ = cmd_count_op(op, csize);
  317.     cmd_put2w(x, y, dp);
  318.     cmd_put2w(w1, height, dp);
  319.     pcls->rect = rect;
  320.     }
  321.     } END_RECTS;
  322.     return 0;
  323. }
  324.  
  325. int
  326. clist_copy_color(gx_device * dev,
  327.          const byte * data, int data_x, int raster, gx_bitmap_id id,
  328.          int x, int y, int width, int height)
  329. {
  330.     gx_device_clist_writer * const cdev =
  331.     &((gx_device_clist *)dev)->writer;
  332.     int depth = dev->color_info.depth;
  333.     int y0;
  334.     int data_x_bit;
  335.     /* We can't know what colors will be used: assume the worst. */
  336.     gx_color_index colors_used = ((gx_color_index)1 << depth) - 1;
  337.  
  338.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  339.     y0 = y;
  340.     data_x_bit = data_x * depth;
  341.     FOR_RECTS {
  342.     int dx = (data_x_bit & 7) / depth;
  343.     int w1 = dx + width;
  344.     const byte *row = data + (y - y0) * raster + (data_x_bit >> 3);
  345.     int code;
  346.  
  347.     pcls->colors_used.or |= colors_used;
  348.     TRY_RECT {
  349.         code = cmd_disable_lop(cdev, pcls);
  350.         if (code >= 0)
  351.         code = cmd_disable_clip(cdev, pcls);
  352.     } HANDLE_RECT(code);
  353.     if (pcls->color_is_alpha) {
  354.         byte *dp;
  355.  
  356.         TRY_RECT {
  357.         code =
  358.             set_cmd_put_op(dp, cdev, pcls, cmd_opv_set_copy_color, 1);
  359.         } HANDLE_RECT(code);
  360.         pcls->color_is_alpha = 0;
  361.     }
  362. copy:{
  363.         gx_cmd_rect rect;
  364.         int rsize;
  365.         byte op = (byte) cmd_op_copy_color_alpha;
  366.         byte *dp;
  367.         uint csize;
  368.         uint compress;
  369.  
  370.         rect.x = x, rect.y = y;
  371.         rect.width = w1, rect.height = height;
  372.         rsize = (dx ? 3 : 1) + cmd_size_rect(&rect);
  373.         TRY_RECT {
  374.         code = cmd_put_bits(cdev, pcls, row, w1 * depth,
  375.                     height, raster, rsize,
  376.                     1 << cmd_compress_rle, &dp, &csize);
  377.         } HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);
  378.         compress = (uint)code;
  379.         if (code < 0) {
  380.         /* The bitmap was too large; split up the transfer. */
  381.         if (height > 1) {
  382.             /* Split the transfer by reducing the height.
  383.              * See the comment above FOR_RECTS in gxcldev.h.
  384.              */
  385.             height >>= 1;
  386.             goto copy;
  387.         } else {
  388.             /* Split a single (very long) row. */
  389.             int w2 = w1 >> 1;
  390.  
  391.             NEST_RECT {
  392.             code = clist_copy_color(dev, row, dx,
  393.                         raster, gx_no_bitmap_id,
  394.                         x, y, w2, 1);
  395.             if (code >= 0)
  396.                 code = clist_copy_color(dev, row, dx + w2,
  397.                             raster, gx_no_bitmap_id,
  398.                             x + w2, y, w1 - w2, 1);
  399.             } UNNEST_RECT;
  400.             if (code < 0)
  401.             ERROR_RECT(code);
  402.             continue;
  403.         }
  404.         }
  405.         op += compress;
  406.         if (dx) {
  407.         *dp++ = cmd_count_op(cmd_opv_set_misc, 2);
  408.         *dp++ = cmd_set_misc_data_x + dx;
  409.         }
  410.         *dp++ = cmd_count_op(op, csize);
  411.         cmd_put2w(x, y, dp);
  412.         cmd_put2w(w1, height, dp);
  413.         pcls->rect = rect;
  414.     }
  415.     } END_RECTS;
  416.     return 0;
  417. }
  418.  
  419. int
  420. clist_copy_alpha(gx_device * dev, const byte * data, int data_x,
  421.        int raster, gx_bitmap_id id, int x, int y, int width, int height,
  422.          gx_color_index color, int depth)
  423. {
  424.     gx_device_clist_writer * const cdev =
  425.     &((gx_device_clist *)dev)->writer;
  426.     /* I don't like copying the entire body of clist_copy_color */
  427.     /* just to change 2 arguments and 1 opcode, */
  428.     /* but I don't see any alternative that doesn't require */
  429.     /* another level of procedure call even in the common case. */
  430.     int log2_depth = ilog2(depth);
  431.     int y0;
  432.     int data_x_bit;
  433.  
  434.     fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  435.     y0 = y;
  436.     data_x_bit = data_x << log2_depth;
  437.     FOR_RECTS {
  438.     int dx = (data_x_bit & 7) >> log2_depth;
  439.     int w1 = dx + width;
  440.     const byte *row = data + (y - y0) * raster + (data_x_bit >> 3);
  441.     int code;
  442.  
  443.     pcls->colors_used.or |= color;
  444.     TRY_RECT {
  445.         code = cmd_disable_lop(cdev, pcls);
  446.         if (code >= 0)
  447.         code = cmd_disable_clip(cdev, pcls);
  448.     } HANDLE_RECT(code);
  449.     if (!pcls->color_is_alpha) {
  450.         byte *dp;
  451.  
  452.         TRY_RECT {
  453.         code =
  454.             set_cmd_put_op(dp, cdev, pcls, cmd_opv_set_copy_alpha, 1);
  455.         } HANDLE_RECT(code);
  456.         pcls->color_is_alpha = 1;
  457.     }
  458.     if (color != pcls->colors[1]) {
  459.         TRY_RECT {
  460.         code = cmd_set_color1(cdev, pcls, color);
  461.         } HANDLE_RECT(code);
  462.     }
  463. copy:{
  464.         gx_cmd_rect rect;
  465.         int rsize;
  466.         byte op = (byte) cmd_op_copy_color_alpha;
  467.         byte *dp;
  468.         uint csize;
  469.         uint compress;
  470.  
  471.         rect.x = x, rect.y = y;
  472.         rect.width = w1, rect.height = height;
  473.         rsize = (dx ? 4 : 2) + cmd_size_rect(&rect);
  474.         TRY_RECT {
  475.         code = cmd_put_bits(cdev, pcls, row, w1 << log2_depth,
  476.                     height, raster, rsize,
  477.                     1 << cmd_compress_rle, &dp, &csize);
  478.         } HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);
  479.         compress = (uint)code;
  480.         if (code < 0) {
  481.         /* The bitmap was too large; split up the transfer. */
  482.         if (height > 1) {
  483.             /* Split the transfer by reducing the height.
  484.              * See the comment above FOR_RECTS in gxcldev.h.
  485.              */
  486.             height >>= 1;
  487.             goto copy;
  488.         } else {
  489.             /* Split a single (very long) row. */
  490.             int w2 = w1 >> 1;
  491.  
  492.             NEST_RECT {
  493.             code = clist_copy_alpha(dev, row, dx,
  494.                         raster, gx_no_bitmap_id, x, y,
  495.                         w2, 1, color, depth);
  496.             if (code >= 0)
  497.                 code = clist_copy_alpha(dev, row, dx + w2,
  498.                             raster, gx_no_bitmap_id,
  499.                             x + w2, y, w1 - w2, 1,
  500.                             color, depth);
  501.             } UNNEST_RECT;
  502.             if (code < 0)
  503.             ERROR_RECT(code);
  504.             continue;
  505.         }
  506.         }
  507.         op += compress;
  508.         if (dx) {
  509.         *dp++ = cmd_count_op(cmd_opv_set_misc, 2);
  510.         *dp++ = cmd_set_misc_data_x + dx;
  511.         }
  512.         *dp++ = cmd_count_op(op, csize);
  513.         *dp++ = depth;
  514.         cmd_put2w(x, y, dp);
  515.         cmd_put2w(w1, height, dp);
  516.         pcls->rect = rect;
  517.     }
  518.     } END_RECTS;
  519.     return 0;
  520. }
  521.  
  522. int
  523. clist_strip_copy_rop(gx_device * dev,
  524.          const byte * sdata, int sourcex, uint sraster, gx_bitmap_id id,
  525.              const gx_color_index * scolors,
  526.        const gx_strip_bitmap * textures, const gx_color_index * tcolors,
  527.              int x, int y, int width, int height,
  528.              int phase_x, int phase_y, gs_logical_operation_t lop)
  529. {
  530.     gx_device_clist_writer * const cdev =
  531.     &((gx_device_clist *)dev)->writer;
  532.     gs_rop3_t rop = lop_rop(lop);
  533.     gx_strip_bitmap tile_with_id;
  534.     const gx_strip_bitmap *tiles = textures;
  535.     int y0;
  536.     /* Compute the set of possible colors that this operation can generate. */
  537.     gx_color_index all = ((gx_color_index)1 << dev->color_info.depth) - 1;
  538.     bool subtractive = dev->color_info.num_components == 4; /****** HACK ******/
  539.     gx_color_index S =
  540.     (scolors ? scolors[0] | scolors[1] : sdata ? all : 0);
  541.     gx_color_index T =
  542.     (tcolors ? tcolors[0] | tcolors[1] : textures ? all : 0);
  543.     gs_rop3_t color_rop =
  544.     (subtractive ? byte_reverse_bits[rop ^ 0xff] : rop);
  545.     bool slow_rop;
  546.  
  547.     if (scolors != 0 && scolors[0] != scolors[1]) {
  548.     fit_fill(dev, x, y, width, height);
  549.     } else {
  550.     fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
  551.     }
  552.     /*
  553.      * On CMYK devices, RasterOps must be executed with complete pixels
  554.      * if the operation involves the destination.
  555.      * This is because the black plane interacts with the other planes
  556.      * in the conversion between RGB and CMYK.  Check for this now.
  557.      */
  558.     {
  559.     gs_rop3_t rop_used = rop;
  560.  
  561.     if (scolors && (scolors[0] == scolors[1]))
  562.         rop_used = (scolors[0] == gx_device_black(dev) ?
  563.             rop3_know_S_0(rop_used) :
  564.             scolors[0] == gx_device_white(dev) ?
  565.             rop3_know_S_1(rop_used) : rop_used);
  566.     if (tcolors && (tcolors[0] == tcolors[1]))
  567.         rop_used = (tcolors[0] == gx_device_black(dev) ?
  568.             rop3_know_T_0(rop_used) :
  569.             tcolors[0] == gx_device_white(dev) ?
  570.             rop3_know_T_1(rop_used) : rop_used);
  571.     slow_rop = !(rop == rop3_0 || rop == rop3_1 ||
  572.              rop == rop3_D || rop == rop3_S || rop == rop3_T);
  573.     }
  574.     y0 = y;
  575.     /*
  576.      * We shouldn't need to put the logic below inside FOR/END_RECTS,
  577.      * but the lop_enabled flags are per-band.
  578.      */
  579.     FOR_RECTS {
  580.     const byte *row = sdata + (y - y0) * sraster;
  581.     gx_color_index D = pcls->colors_used.or;
  582.     int code;
  583.  
  584.     pcls->colors_used.or =
  585.         ((rop_proc_table[color_rop])(D, S, T) & all) | D;
  586.     pcls->colors_used.slow_rop |= slow_rop;
  587.     if (rop3_uses_T(rop)) {
  588.         if (tcolors == 0 || tcolors[0] != tcolors[1]) {
  589.         ulong offset_temp;
  590.  
  591.         if (!cls_has_tile_id(cdev, pcls, tiles->id, offset_temp)) {
  592.             /* Change tile.  If there is no id, generate one. */
  593.             if (tiles->id == gx_no_bitmap_id) {
  594.             tile_with_id = *tiles;
  595.             tile_with_id.id = gs_next_ids(1);
  596.             tiles = &tile_with_id;
  597.             }
  598.             TRY_RECT {
  599.             code = clist_change_tile(cdev, pcls, tiles,
  600.                          (tcolors != 0 ? 1 :
  601.                           dev->color_info.depth));
  602.             } HANDLE_RECT_UNLESS(code, code == gs_error_limitcheck);
  603.             if (code < 0) {
  604.             /*
  605.              * The error is a limitcheck: we have a tile that
  606.              * is too big to fit in the command reading buffer.
  607.              * For now, just divide up the transfer into scan
  608.              * lines.  (If a single scan line won't fit, punt.)
  609.              * Eventually, we'll need a way to transfer the tile
  610.              * in pieces.
  611.              */
  612.             uint rep_height = tiles->rep_height;
  613.             gs_id ids;
  614.             gx_strip_bitmap line_tile;
  615.             int iy;
  616.  
  617.             if (rep_height == 1 ||
  618.                 /****** CAN'T HANDLE SHIFT YET ******/
  619.                 tiles->rep_shift != 0
  620.                 )
  621.                 return code;
  622.             /*
  623.              * Allocate enough fake IDs, since the inner call on
  624.              * clist_strip_copy_rop will need them anyway.
  625.              */
  626.             ids = gs_next_ids(min(height, rep_height));
  627.             line_tile = *tiles;
  628.             line_tile.size.y = 1;
  629.             line_tile.rep_height = 1;
  630.             for (iy = 0; iy < height; ++iy) {
  631.                 line_tile.data = tiles->data + line_tile.raster *
  632.                 ((y + iy + phase_y) % rep_height);
  633.                 line_tile.id = ids + (iy % rep_height);
  634.                 /*
  635.                  * Note that since we're only transferring
  636.                  * a single scan line, phase_y is irrelevant;
  637.                  * we may as well use the current tile phase
  638.                  * so we don't have to write extra commands.
  639.                  */
  640.                 NEST_RECT {
  641.                 code = clist_strip_copy_rop(dev,
  642.                     (sdata == 0 ? 0 : row + iy * sraster),
  643.                     sourcex, sraster,
  644.                     gx_no_bitmap_id, scolors,
  645.                     &line_tile, tcolors,
  646.                     x, y + iy, width, 1,
  647.                     phase_x, pcls->tile_phase.y, lop);
  648.                 } UNNEST_RECT;
  649.                 if (code < 0)
  650.                 ERROR_RECT(code);
  651.             }
  652.             continue;
  653.             }
  654.             if (phase_x != pcls->tile_phase.x ||
  655.             phase_y != pcls->tile_phase.y
  656.             ) {
  657.             TRY_RECT {
  658.                 code = cmd_set_tile_phase(cdev, pcls, phase_x,
  659.                               phase_y);
  660.             } HANDLE_RECT(code);
  661.             }
  662.         }
  663.         }
  664.         /* Set the tile colors. */
  665.         TRY_RECT {
  666.         code =
  667.             (tcolors != 0 ?
  668.              cmd_set_tile_colors(cdev, pcls, tcolors[0], tcolors[1]) :
  669.              cmd_set_tile_colors(cdev, pcls, gx_no_color_index,
  670.                      gx_no_color_index));
  671.         } HANDLE_RECT(code);
  672.     }
  673.     TRY_RECT {
  674.         code = 0;
  675.         if (lop != pcls->lop)
  676.         code = cmd_set_lop(cdev, pcls, lop);
  677.         if (code >= 0)
  678.         code = cmd_enable_lop(cdev, pcls);
  679.     } HANDLE_RECT(code);
  680.  
  681.     /* Set lop_enabled to -1 so that fill_rectangle / copy_* */
  682.     /* won't attempt to set it to 0. */
  683.     pcls->lop_enabled = -1;
  684.     NEST_RECT {
  685.         if (scolors != 0) {
  686.         if (scolors[0] == scolors[1])
  687.             code = clist_fill_rectangle(dev, x, y, width, height,
  688.                         scolors[1]);
  689.         else
  690.             code = clist_copy_mono(dev, row, sourcex, sraster, id,
  691.                        x, y, width, height,
  692.                        scolors[0], scolors[1]);
  693.         } else
  694.         code = clist_copy_color(dev, row, sourcex, sraster, id,
  695.                     x, y, width, height);
  696.     } UNNEST_RECT;
  697.     pcls->lop_enabled = 1;
  698.     if (code < 0)
  699.         ERROR_RECT(code);
  700.     } END_RECTS;
  701.     return 0;
  702. }
  703.